<?php

namespace zhtfrontend\controllers;

use doumiaoduo\buildform\BuildForm;
use zhtfrontend\components\bases\BaseController;
use common\models\datas\IntegralConfigItem;
use common\models\datas\IntegralConfig;
use yii\web\Response;
use common\components\extenders\AjaxObject;
use common\components\helpers\Fun;
use yii\helpers\ArrayHelper;
use yii\web\BadRequestHttpException;
use yii\web\NotFoundHttpException;
use common\components\helpers\Util;
use common\components\actions\DeleteAction;

class IntegralconfigController extends BaseController
{

    public function actionIndex()
    {
        $data = IntegralConfigItem::find()->where(['site_id' => Fun::getSiteId()])->orderBy('id ASC')->all();
        $array = Fun::asArray($data, ['config_name', 'admin', 'zhtfrontend_operate']);

        return $this->render('index', ['data' => $array]);
    }

    public
    function actionCreate()
    {
        $this->layout = false;
        $model = new IntegralConfigItem();
        $form = new BuildForm('@app/components/forms/integralconfigitemForm.php');
        $form->loadModel(['integralconfigitem' => $model]);
        $mechanic = IntegralConfig::find()->all();
        $form->setItems('integralconfigitem', 'config_id', ArrayHelper::map($mechanic, 'id', 'name'));
        if (\Yii::$app->request->isAjax && \Yii::$app->request->post('ajax') == 'attr') {
            \Yii::$app->response->format = Response::FORMAT_JSON;
            $id = \Yii::$app->request->post('configitem');
            $content = IntegralConfig::find()->where(['id' => $id])->one();
            $ajax_object = new AjaxObject();
            $ajax_object->data = Util::array2text(unserialize($content->variable));
            return $ajax_object;
        }
        if ($form->loadData(\Yii::$app->request->post()) && $form->validate()) {
            $model = $form->getModel('integralconfigitem');
            $model->site_id = Fun::getSiteId();
            $model->admin_id = 1;
            if ($model->save(false)) {
                return $this->redirectMessage('添加成功', Fun::url(['integralconfig/index']), true);
            }
        }
        return $this->render('create', ['form' => $form->render()]);
    }

    public
    function actionUpdate()
    {
        $this->layout = false;
        if (\Yii::$app->request->isAjax && \Yii::$app->request->post('ajax') == 'attr') {
            \Yii::$app->response->format = Response::FORMAT_JSON;
            $id = \Yii::$app->request->post('configitem');
            $content = IntegralConfig::find()->where(['id' => $id])->one();
            $ajax_object = new AjaxObject();
            $ajax_object->data = Util::array2text(unserialize($content->variable));
            return $ajax_object;
        }
        if (\Yii::$app->request->get('id') == null) {
            throw new BadRequestHttpException(\Yii::t('zhtfrontend', '信息不存在'));
        }
        $model = IntegralConfigItem::findOne(\Yii::$app->request->get('id'));
        if ($model == null) {
            throw new NotFoundHttpException(\Yii::t('zhtfrontend', '信息不存在'));
        }
        $form = new BuildForm('@app/components/forms/integralconfigitemForm.php');
        $form->loadModel(['integralconfigitem' => $model]);
        $intergralconfig_cache = Fun::cache('integralconfig_cache');
        if ($intergralconfig_cache == null) {
            throw new BadRequestHttpException(\Yii::t('zhtfrontend', '信息不存在'));
        }
//        $form->setItems('integralconfigitem', 'config_id', ArrayHelper::map($intergralconfig_cache, 'id', 'name'));
        if ($form->loadData(\Yii::$app->request->post()) && $form->validate()) {
            $model = $form->getModel('integralconfigitem');
            if ($model->save(false)) {
                return $this->redirectMessage(\Yii::t('zhtfrontend', '修改成功'), Fun::url(['integralconfig/index']), 'success');
            }
        }
        return $this->render('create', ['form' => $form->render()]);
    }

    public
    function actions()
    {
        return [
            'delete' => [
                'class' => DeleteAction::className(),
                'modelClass' => IntegralConfigItem::className(),
                'message' => ['success' => '删除成功！'],
                'redirectTo' => 'integralconfig/index',
            ],
        ];
    }

}
